package forms;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.RowSorter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;
import javax.swing.JSpinner;
import javax.swing.JButton;

import dto.UserDTO;
import services.user.User;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Collections;
import java.util.Vector;

public class TestTable extends JFrame {

	private JPanel contentPane;
	private JTable table;
	private JButton btnNewButton;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TestTable frame = new TestTable();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public TestTable() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 746, 459);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JScrollPane scrollPane = new JScrollPane();
		contentPane.add(scrollPane, BorderLayout.CENTER);
		
		table = new JTable();
		/*
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null},
				{null, null, null},
				{null, null, null},
			},
			new String[] {
				"name", "classes", "Age"
			}
		));
		*/
		Vector<Vector<Object>> tableDatas = new User().getAllUsers();
		
		Vector<String> colums = new Vector<String>();
		colums.add("user_name");
		colums.add("password");
		
		DefaultTableModel tableModel = new DefaultTableModel(tableDatas,colums);
		RowSorter sorter = new TableRowSorter<DefaultTableModel>(tableModel);
		table.setRowSorter(sorter);
		table.setModel(tableModel);
		scrollPane.setViewportView(table);
		
		btnNewButton = new JButton("New button");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//System.out.println(new User().getAllUsers());
				Vector<Vector<Object>> tableDatas = new User().getAllUsers();
				
				Vector<String> colums = new Vector<String>();
				colums.add("user_name");
				colums.add("password");
				
				DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
				tableModel.setDataVector(tableDatas, colums);
//				RowSorter sorter = new TableRowSorter<DefaultTableModel>(tableModel);
//				table.setRowSorter(sorter);
//				table.setModel(tableModel);
				table.repaint();
				table.updateUI();
			}
		});
		contentPane.add(btnNewButton, BorderLayout.NORTH);
	}

}
